package com.tojaoomy.car.service.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;

import com.tojaoomy.car.controller.vo.People;
import com.tojaoomy.car.service.PeopleService;

@Service
public class PeopleServiceImpl implements PeopleService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public People save(final People people) {
		KeyHolder keyHolder = new GeneratedKeyHolder();

		final String sql = "insert into people(subject,start_date,end_date,description) values(?,?,?,?)";
		
		jdbcTemplate.update(new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
				ps.setString(1, people.getSubject());
				ps.setDate(2, people.getStartDate());
				ps.setDate(3, people.getEndDate());
				ps.setString(4, people.getDescription());
				return ps;
			}
		}, keyHolder);

		people.setId(keyHolder.getKey().intValue());
		return people;
	}

	@Override
	public People queryById(int id) {
		return null;
	}

	public static void main(String[] args) {
	}

}
